import java.util.Arrays;
public class Test8 {
    public static boolean findThreeOddNumber(int[] arr) {//判断数组中是否有三个连续的奇数
        for (int i = 0; i <arr.length ; i++) {
            if(arr[i] % 2 != 0 && arr[i+1] % 2 != 0 && arr[i+2] % 2 != 0){
                return true;
            }
        }return false;
    }
    public static void main8(String[] args) {
        int[] arr = {1,3,5,2,4,5,8,6};
        System.out.println(findThreeOddNumber(arr));
        int[] arr1 = {2,4,6,8};
        System.out.println(findThreeOddNumber(arr1));
    }
    public static int become(int[] arr,int n){//找到出现超过n/2次的元素
    int left = 0;               //被查找的元素
    int right = arr.length - 1; //遍历整个数组查找left元素
        int count = 0;
    while (true){
      if(arr[right]==arr[left]){
              count++;     //相同则计数器++
              if(right != 0){  //下标不能小于0
                  right--;
              }
          }
      else {
          if(right != 0){
              right--;
          }
      }
      if(count > n){        //如果次数大于数组长度的1/2，跳出循环
          break;
      }
        if(right == 0){//当左下标指向0，且没有跳出循环，则说明它出现的次数没有超过1/2，查找下一个数，另左下标+1，右下标回到数组的最右端，计数器归0
            left++;
            right = arr.length - 1;
            count = 0;
        }
     }return arr[left];
    }

    public static void main7(String[] args) {
        int[] arr = {1,2,3,2,4,2,2,2};
        int n = arr.length / 2;
        System.out.println(become(arr,n));
    }
    public static int becomeOnce(int[] arr) { //找出只出现一次的元素（类似二分查找）
        int left = 0;                       //left代表被查找数
        int right = arr.length - 1;         //right从数组最右端向左查找
            while (right != -1) {//当查找到下标为0之后跳出循环

                if (arr[left] == arr[right] && left != right) {         //值相等且坐标不能相同，如果符合则开始下一个数的查找，右下标返回数组最右端
                    left++;
                    right = arr.length - 1;
                    continue;
                }
                right--;

            }
            return arr[left];
    }

    public static void main6(String[] args) {
        int[] arr = {1,2,1,2,4,3,3};
        System.out.println(becomeOnce(arr));

    }
    public static String findTarget(int[] arr,int t1){//求两个整数相加所得的目标值，通过字符串的方式输出（返回字符串类型）
        String ret = "[";
        for (int i = 0; i < arr.length; i++) {//遍历两次数组
            for (int j = 0; j < arr.length; j++) {
                if (arr[i] + arr[j] == t1){//相加等于target
                    ret += i + "," + j;
                    ret +="]";
                    return ret;
                }
            }
        }
        ret += "找不到]";
        return ret;
    }
    public static void main5(String[] args) {
        int[] arr = {1,6,5,7,9,8};
        String ret = findTarget(arr,9);
        System.out.println(ret);
    }
    public static void bubbleSort(int[] arr){//冒泡排序
        int tmp = 0;
        for (int i = 0; i < arr.length - 1; i++) {
            //i是遍历的趟数
            //j是调换位置的次数
            for (int j = 0; j < arr.length - i -1; j++) {
                if(arr[j] > arr[j+1]){
                    tmp = arr[j];
                   arr[j] = arr[j+1];
                   arr[j+1] = tmp;
                }
            }

        }
    }
    public static void main4(String[] args) {
        int[] arr1 = {1,7,3,2,88,99,77,65};
        bubbleSort(arr1);
        System.out.println(Arrays.toString(arr1));
    }
    public static void sort(int[] arr){//将奇数放在偶数的前面
        int tmp = 0;
        for (int i = 0; i < arr.length / 2; i++) {
            for (int j = 0; j <=  arr.length - 2; j++) {//不仅要考虑每次调整数字的次数还要考虑遍历到的位置
                if(arr[j] % 2 == 0 && arr[j+1] % 2 != 0){//调换位置条件：前面是偶数，后面是奇数
                tmp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = tmp;
                }

            }
        }
    }
    public static void main3(String[] args) {
            int[] arr = {1,2,3,4,5,6,7,8,9,10,11,12};
            sort(arr);
        System.out.println(Arrays.toString(arr));
    }
    public static  double avg(int[] arr){
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];      //计算总和
        }
        return (double) sum/5.0;//通过强转转换为double类型
    }
    public static void main2(String[] args) {
        int[] arr1 = {1,2,3,4,5};
        System.out.println(avg(arr1));
    }
    public static int[] transfrom(int[] arr){//将数组作为函数返回值
        int[] ret = new int[arr.length];
        //遍历数组
        for (int i = 0; i < arr.length; i++) {
            ret[i] = arr[i] * 2;        //将arr[i]*2
        }return ret;
    }
    public static void main1(String[] args) {//将数组的每个元素*2
        int[] arr = {1,2,3};
        int[] ret = transfrom(arr);
        System.out.println(Arrays.toString(ret));       //将数组转换为字符串输出
    }
}
